
class Survey < ActiveRecord::Base
  belongs_to :survey_type ,:class_name => 'SurveyType', :foreign_key => "test_type_id" 
  belongs_to :user
  has_many :parts, :dependent => :destroy
  has_many :dimens, :dependent => :destroy
  has_many :questions, :order => :position, :dependent => :destroy
  has_many :trend_scales, :dependent => :destroy
  has_many :choices,:dependent => :destroy
  has_many :results, :dependent => :destroy
  has_many :norms, :dependent => :destroy
  has_many :images, :dependent => :destroy
  has_many :reports 
  has_many :original_results
  has_many :pre_original_results
  has_one :survey_count_method, :dependent => :destroy
  has_one :norm_info, :dependent => :destroy
  has_and_belongs_to_many :users, :join_table => "users_surveys"
  has_many :orgs
  has_many :survey_records
  has_many :pre_survey_records
  
  validates_presence_of :name, :description
  #validates_presence_of :title ,:message =>"测试标题不能为空"
  
#	before_destroy :delete_choies
#
#	def delete_choies
#		Choice.delete_all(:test_id => self.id)
#	end

  def parts_size
    Question.count(:conditions => ["test_id = ? and marker = ?", self.id, QUESTION_MARKER_PART])
  end

  def self.prof_surveys
    Survey.find_all_by_professional(true)
  end

  def self.prof_online_surveys
    Survey.find_all_by_professional_and_status_and_deleted(true, TEST_STATUS_FINISHED, false)
  end

  def question_num
    if self.has_part == 1
      question_num = 0
      self.parts.each do |p|
        question_num += p.questions.size
      end
      question_num
    else
      self.questions.size
    end
  end

  #对有分量表的测试进行分页
  def paging
    count = 0
    total = 0
    pages = []
    page = []
    page_size = self.page_size || QUESTION_PAGE_SIZE

    self.parts.each do |part|
      page.push({:item => part, :type => 'part'})
      
      part.questions.each do |question|
        page.push({:item => question, :type => 'question'})
        count += 1

        
        if count == page_size or total == self.question_num-1
          pages.push(page)
          count = 0
          page = []
        end

        total += 1
      end
    end

    return pages
  end

  def paging_no_part
    page_size = self.page_size || QUESTION_PAGE_SIZE
    page_num = (self.question_num%page_size == 0 and self.question_num-page_size >= 0) ? self.question_num/page_size : self.question_num/page_size + 1

    pages = []
    (0...page_num).each do |i|
      pages[i] = self.questions[page_size*i..(page_size*i+page_size-1)]
    end
    survey = {}
    survey[:pages] = pages
    return survey
  end

  def self.c_surveys(per_page, page)
    sql = "select * from surveys where " +
          "deleted is null and  t_type = '#{TEST_TYPE_COUNT}' order by created_at desc "
    Survey.paginate_by_sql(sql,{:per_page => per_page, :page=>page})
  end
  
  def self.t_surveys(per_page, page)
    sql = "select * from surveys where " +
          "deleted is null and  t_type = '#{TEST_TYPE_TREND}' order by created_at desc "
    Survey.paginate_by_sql(sql,{:per_page => per_page, :page=>page})
  end

  def self.used_surveys(per_page, page)
    sql = "select * from surveys where status = '启用' and " +
          "deleted is null and  t_type like '%测试%' order by created_at desc "
    Survey.paginate_by_sql(sql,{:per_page => per_page, :page=>page})
  end

  def self.small_surveys_count
    self.count(:conditions => [" t_type = 'small' and deleted is null"])
  end

  def type_is_count
    self.t_type == '计分测试'
  end

  def type_is_trend
    self.t_type == '倾向测试'
  end

  def c_report(params)
    total = 0
    self.questions.each do |question|
      q_value = params["question#{question.id}value"].to_i
      total = total + q_value
    end
puts "total===============#{total}"
    
    #报告
    report = {}
    report[:type] = 'c_report'
    report[:expl] = Norm.find(:first, 
      :conditions => ['survey_id = ? and begin_num <= ? and end_num >= ?', params[:id], total, total])
    return report
  end

  def t_report(params)
    trends = []
    self.dimens.each do |d|
      dimen = {}
      dimen[:dimen] = d
      dimen[:value] = 0
      d.questions.each do |q|
        dimen[:value] = dimen[:value] + params["question#{q.id}value"].to_i
      end
      trends.push(dimen)
    end

    max = trends.max {|a, b| a[:value] <=> b[:value]}
    report = max
    report[:type] = 't_report'
    return report
  end

  
end


